package mmc.cell3;

import java.util.ArrayList;
import java.util.List;

import mmc.Constants;
import mmc.cell.BlockIterator;
import mmc.cell.ServerCell;

class BlockIteratorImpl implements BlockIterator{
	private List<ServerCell> affectedChunks = new ArrayList<ServerCell>();
	private List<BlockDefinition> blockDef = new ArrayList<BlockDefinition>();
	private int pos=-1;
	
	public void addAffectedChunk(ServerCell c){
		affectedChunks.add(c);
	}
	
	public List<ServerCell> getAffectedChunks(){
		return affectedChunks;
	}
	
	public void addDefinition(ServerCell c, int bx, int by, int bz){
		blockDef.add(new BlockDefinition(c,bx,by,bz));
	}
	
	public boolean next(){
		return ++pos<blockDef.size();
	}
	
	public byte getBlockId(){
		BlockDefinition def = blockDef.get(pos);
		return def.c.getValue(def.bx, def.by, def.bz);
	}
	
	public void setBlockId(byte blockId){
		BlockDefinition def = blockDef.get(pos);
		if(def.c.isCompressed())
			def.c.decompress();
		def.c.block[def.bx*Constants.CELL_SIZE2+def.by*Constants.CELL_SIZE+def.bz] = blockId;
	}
	
	static class BlockDefinition{
		ServerCell c;
		int bx, by, bz;
		
		public BlockDefinition(ServerCell c, int bx, int by, int bz){
			this.c=c;
			this.bx=bx;
			this.by=by;
			this.bz=bz;
		}
	}
}
